<?php


namespace App\Service\OrderService\OrderInterfaceService;

use App\Model\CommodityModel\SupplierCompany;
use App\Service\BaseService;
use App\Model\PssModel\OrderSupplier;
use App\Service\Code\Normal;

/**
 * 云仓通知上游撤单接口
 */
class OrderRevoke extends BaseService
{
    /**
     * 订单售后处理
     */
    public function index( array $param = [] )
    {
        if( empty($param['requestData']) ) {
            return Normal::returnCode(Normal::FAIL,"订单数据不能为空");
        }
        $requestData = $param['requestData'];
        if( empty($requestData['orderSn']) ) {
            return Normal::returnCode(Normal::FAIL,"渠道商订单orderSn不能为空");
        }

        //查询售后订单
        $return = $this->getSupplierOrderLists($param);
        if( $return['code'] != Normal::SUC ) {
            return $return;
        }
        
        $supplierOrder  = $return['data']['supplierOrder'];
        $supplier       = $return['data']['supplier'];
        return $this->singleOrderRevoke($supplierOrder,$supplier);
    }
    
    /**
     * 撤单通知上游
     * @param array $supplierOrder
     * @param array $supplier
     * @return mixed
     */
    public function singleOrderRevoke(array $supplierOrder,array $companyInfo)
    {
        if( empty($supplierOrder) ) {
            return Normal::returnCode(Normal::FAIL,"供应商订单信息不能为空");
        }
        
        if( empty($companyInfo) ) {
            return Normal::returnCode(Normal::FAIL,"供应商信息不能为空");
        }
        
        $company_code   = $companyInfo['company_code'];
        $supplierClass  = ucFirst( $companyInfo['OrderDockMode'] == 1 ? 'normal' : $companyInfo['company_code'] );
        $serviceClass   = "App\\Service\\OrderService\\OrderRouteService\\{$supplierClass}";
        if( !class_exists($serviceClass) ) {
            return Normal::returnCode(Normal::FAIL,"{$serviceClass} not existed");
        }
        $requestMethod = 'OrderRevoke';
        if( !method_exists($serviceClass,$requestMethod) ) {
            return Normal::returnCode(Normal::FAIL,"{$serviceClass} {$requestMethod} methods not existed");
        }
        return $serviceClass::instance()->$requestMethod([
            'supplierOrder' => $supplierOrder,
            'companyInfo'   => $companyInfo
        ]);
    }
    
    /**
     * 获取供应商列表
     * @param array $param
     */
    public function getSupplierOrderLists( array $param = [] )
    {
        $bid = $param['binfo']['bid'];
        $requestData = $param['requestData'];
        $query = OrderSupplier::instance()
        ->select([
            'order_sn',
            'company_id',
            'order_push_status'
        ])
        ->where('bid',$bid)
        ->where('order_sn',$requestData['orderSn'])
        ->first();
        
        if( empty($query) ) {
            return Normal::returnCode(Normal::SUC,"当前需要撤单的数据不存在，无须通知处理");
        }
        
        $supplierOrder = $query->toArray();
        if( $supplierOrder['order_push_status'] != 1 ) {
            return Normal::returnCode(Normal::SUC,"当前数据未成功推送关务，无须通知处理");
        }
        
        $SupplierCompanyLists = SupplierCompany::instance()->getSupplierListByIds([(string)$supplierOrder['company_id']]);
        return Normal::returnCode(Normal::SUC,"订单数据获取成功",[
            'supplierOrder' => $supplierOrder,
            'supplier' => current($SupplierCompanyLists)
        ]);
    }
}